// pages/login/login.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 眼睛的控制变量
    seePSW: false,
    //储存用户输入的电话密码
    phone: "",
    password: "",
    //判断电话和密码的格式
    phoneFlag: "",
    passwordFlag: "",
    //控制弹出层
    show: false,
    //注册的昵称
    registerName: "",
    //注册密码内容是否可见
    registerSeePSW: "false",
    //储存注册用户输入的电话密码
    registerPhone: "",
    registerPassword: "",
    //判断注册电话、密码和注册昵称的格式
    registerPhoneFlag: "",
    registerPasswordFlag: "",
    registerNameFlag: "",
  },

  //显示密码
  watchPSW(){
    let seePSW=this.data.seePSW
    this.setData({
      seePSW: !seePSW
    })
  },

  //显示注册密码
  registerWatchPSW(){
    let registerSeePSW = this.data.registerSeePSW;
    this.setData({
      registerSeePSW: !registerSeePSW
    })
  },

  //提交数据到登录接口
  login(){
    let password = this.data.password;
    let phone = this.data.phone;
    wx.request({
      url: 'https://kf.webxyq.com/login',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      data: {
        appkey: app.globalData.appkey,
        password: this.data.password,
        phone: this.data.phone
      },
      success: res => {
        if (res.data.code == 201) {
          wx.showToast({
            title: res.data.msg,
            icon: "error"
          })
        }else if(res.data.code == 202) {
          wx.showToast({
            title: res.data.msg,
            icon: "error"
          })
        }else if(res.data.code){
          //登录成功
          wx.setStorage({
            key:"_token",
            data: res.data.token
          })
          wx.showToast({
            title: '登录成功',
            icon: "success"
          })
          setTimeout(()=>{
            wx.switchTab({
              url:'/pages/home/home'
            })
          },1000);
        }else{
          wx.showToast({
            title: '未知错误',
            icon: "error"
          })
        }
      }
    })
  },

  //判断电话格式规则
  validatePhone(phone){
    // 中国大陆手机号正则
    const regex = /^1[3-9]\d{9}$/;
    return regex.test(phone);
  },
  //获取用户输入电话并判断
  inputPhone(e){
    let phone = e.detail;
    this.phoneIF(phone);
  },

  phoneIF(phone){
    if(this.validatePhone(phone)){
      this.setData({
        phone: phone, // 获取输入框当前值
        phoneFlag: ""
      });
    }else if(phone == ""){
      this.setData({
        phoneFlag: "手机号必填",
        phone: ""
      });
    }else{
      this.setData({
        phoneFlag: "手机号格式不正确",
        phone: ""
      })
    }
  },

  //判断密码格式规则
  validatePassword(password){
    const regex = /^[A-Za-z][A-Za-z0-9_]{5,15}$/;
    return regex.test(password);
  },

  //获取用户输入密码并判断
  inputPSW(e) {
    let password = e.detail;
    this.passwordIF(password);
  },

  passwordIF(password){
    if(this.validatePassword(password)){
      this.setData({
        password: password,
        passwordFlag: ""
      });
    }else if(password == ""){
      this.setData({
        passwordFlag: "密码必填",
        password: "",
      })
    }else{
      this.setData({
        passwordFlag: "密码支持数字、字母和下划线组合，且必须以字母开头，6-16位",
        password: "",
      })
    }
  },

  //控制弹出层
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },

  //获取注册用户输入电话并判断
  registerInputPhone(e) {
    let phone = e.detail;
    this.registerPhoneIF(phone);
  },

  registerPhoneIF(phone){
    if (this.validatePhone(phone)) {
      this.setData({
        registerPhone: phone, // 获取输入框当前值
        registerPhoneFlag: " "
      });
    }else if (phone == "") {
      this.setData({
        registerPhoneFlag: "手机号必填",
        registerPhone: ""
      });
    }else {
      this.setData({
        registerPhoneFlag: "手机号格式不正确",
        registerPhone: ""
      })
    }
  },
  //获取注册用户输入密码并判断
  registerInputPSW(e) {
    let password = e.detail;
    this.registerPasswordIF(password);
  },

  registerPasswordIF(password) {
    if (this.validatePassword(password)) {
      this.setData({
        registerPassword: password,
        registerPasswordFlag: ""
      });
    }
    else if (password == "") {
      this.setData({
        registerPasswordFlag: "密码必填",
        registerPassword: "",
      })
    }
    else {
      this.setData({
        registerPasswordFlag: "密码支持数字、字母和下划线组合，且必须以字母开头，6-16位",
        registerPassword: "",
      })
    }
  },

  //判断注册昵称格式规则
  validateName(name){
    const regex = /^[\u4e00-\u9fa5a-zA-Z0-9]{1,10}$/;
    return regex.test(name);
  },

  //获取用户输入的昵称
  registerInputName(e) {
    let name = e.detail;
    this.registerNameIF(name);
  },

  registerNameIF(name){
    if (this.validateName(name)) {
      this.setData({
        registerNameFlag: "",
        registerName: name
      })
    }else if (name == "") {
      this.setData({
        registerNameFlag: "昵称必填",
        registerName: ""
      })
    }else {
      this.setData({
        registerNameFlag: "昵称支持中英文数字组合，1-10位",
        registerName: ""
      })
    }
  },

  //点击注册按钮
  register(){
    let name = this.data.registerName;
    let password = this.data.registerPassword;
    let phone = this.data.registerPhone;
    if (name == "" || password == "" || phone == "") {
      this.registerNameIF(name);
      this.registerPasswordIF(password);
      this.registerPhoneIF(phone);
    }else{
      wx.request({
        url: 'https://kf.webxyq.com/register',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        data: {
          appkey: app.globalData.appkey,
          password: this.data.registerPassword,
          phone: this.data.registerPhone,
          nickName: this.data.registerName
        },
        success: res => {
          console.log(res);
          if (res.data.code == 102) {
            wx.showToast({
              title: res.data.msg,
              icon: "error"
            })
          }else if(res.data.code == 100){
            //注册成功
            wx.showToast({
              title: '注册成功',
              icon: "success"
            })
            setTimeout(()=>{
              this.onClose();
            },1000);
          }else{
            wx.showToast({
              title: '未知错误',
              icon: "error"
            })
          }
        }
      })
    }
  },

  goToForgotPassword: function() {
    wx.navigateTo({
        url: '/pages/forgotPassword/forgotPassword',
    })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  toHome:()=>{
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})